<template>
  <div>
    <!-- 头部 -->
    <el-card>
      <!-- 
          三级联动的地方

         -->
      <Category @getCategoryId="getCategoryId"></Category>
    </el-card>
    <!-- 底部 -->
    <el-card style="margin: 20px 0">
      <!-- 场景0 -->
      <div v-show="scene == 0">
        <el-button type="primary" icon="el-icon-plus" :disabled="!category3Id" @click="addSpu"
          >添加SPU</el-button
        >
        <!-- 展示SPU -->
        <el-table
          style="width: 100%; margin: 20px 0"
          border
          :data="records"
          v-loading="loading"
        >
          <el-table-column type="index" label="序号" width="80" align="center">
          </el-table-column>
          <el-table-column prop="spuName" label="SPU名称" width="width">
          </el-table-column>
          <el-table-column prop="description" label="SPU描述" width="width">
          </el-table-column>
          <el-table-column label="操作" width="width">
            <template slot-scope="{ row, $index }">
              <el-button
                type="success"
                size="mini"
                icon="el-icon-plus"
                title="添加SKU"
              ></el-button>
              <el-button
                type="warning"
                size="mini"
                icon="el-icon-edit"
                title="修改SKU"
                @click="updateSpu(row)"
              ></el-button>
              <el-button
                type="info"
                size="mini"
                icon="el-icon-info"
                title="查看SPU的全部的SKU"
              ></el-button>
              <el-button
                type="danger"
                size="mini"
                icon="el-icon-delete"
                title="删除SKU"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 
         @size-change="handleSizeChange"
        -->
        <el-pagination
          style="text-align: center"
          :current-page="page"
          :page-sizes="[3, 5, 10]"
          :page-size="limit"
          prev-text="上一页"
          next-text="下一页"
          layout="prev, pager, next, jumper,->,sizes,total"
          background
          :page-count="7"
          :total="total"
          @current-change="getSpuList"
          @size-change="handleSizeChange"
        >
        </el-pagination>
      </div>
      <SpuForm v-show="scene == 1" @changeScene="changeScene" ref="spu"></SpuForm>
      <SkuForm v-show="scene == 2" ></SkuForm>
    </el-card>
  </div>
</template>

<script>
// 引入子组件
import SpuForm from "./SpuForm";
import SkuForm from "./SkuForm";
export default {
  components: {
    SpuForm,
    SkuForm,
  },
  name: "Spu",
  data() {
    return {
      // 加载效果
      loading: false,
      category1Id: "",
      category2Id: "",
      category3Id: "",
      page: 1, //分页器当前页码
      limit: 3, //一页几条数据
      total: 0, //
      records: [], // 存储数据列表
      // 场景切换
      scene: 0, //
    };
  },
  methods: {
    // 三级联动的自定义方法
    getCategoryId({ id, flag }) {
      switch (flag) {
        case 1:
          this.category1Id = id;
          this.category2Id = "";
          this.category3Id = "";
          break;
        case 2:
          this.category2Id = id;
          this.category3Id = "";
          break;
        case 3:
          this.category3Id = id;
          this.getSpuList();
          this.loading = true;

          break;
      }
    },
    // 获取SPU(类的)列表进行展示
    async getSpuList(pager = 1) {
      this.page = pager;
      this.loading = true;
      let { category3Id, page, limit } = this;
      let result = await this.$ajax.spu.reqSpuList(page, limit, category3Id);
      if (result.code == 200) {
        // 存储数据
        this.total = result.data.total;
        this.records = result.data.records;
        this.loading = false;
      }
    },
    // 点击修改一页几条数据
    handleSizeChange(limit) {
      this.limit = limit;
      this.getSpuList();
    },
    // 添加Spu类的回调
    addSpu(){
      this.scene=1;
    },
    // 修改Spu类的回调
    updateSpu({id}){
      this.scene=1;
      // ref获取子组件
      this.$refs.spu.initData(id);
    },
    // 切换场景 子组件的自定义事件
    changeScene(scene) {
      this.scene = scene; 
    }
  },
};
</script>

<style>
</style>